<template>
  <div class="ape-admin-container">
    <section class="ape-admin-layout" v-if="showSearch">
      <el-form ref="queryForm" :inline="true" :model="queryParams" size="small">
        <el-form-item label="表类型">
          <el-select v-model="queryParams.tableType" placeholder="请选择表类型">
            <el-option label="全部" :value="null"> </el-option>
            <el-option label="单表" :value="1"> </el-option>
            <el-option label="主表" :value="2"> </el-option>
            <el-option label="附表" :value="3"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="表名">
          <el-input
            v-model="queryParams.tableName"
            placeholder="表名"
          ></el-input>
        </el-form-item>
        <el-form-item label="表描述">
          <el-input
            v-model="queryParams.tableDescribe"
            placeholder="表描述"
          ></el-input>
        </el-form-item>
        <el-form-item label="同步状态">
          <el-select
            v-model="queryParams.syncStatus"
            placeholder="请选择同步状态"
          >
            <el-option label="全部" :value="null"></el-option>
            <el-option label="已同步" :value="1"></el-option>
            <el-option label="未同步" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch" icon="el-icon-search">
            搜索
          </el-button>
          <el-button type="warning" @click="resetQuery" icon="el-icon-refresh">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </section>
    <section
      class="ape-admin-layout"
      type="flex"
      align="space-between"
      position="top"
    >
      <el-row>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >
          新增
        </el-button>
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >
          修改
        </el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >
          删除
        </el-button>
      </el-row>
      <el-row>
        <el-tooltip
          effect="dark"
          :content="showSearch ? '隐藏搜索' : '显示搜索'"
          placement="top"
        >
          <el-button
            icon="el-icon-search"
            size="mini"
            circle
            @click="showSearch = !showSearch"
          ></el-button>
        </el-tooltip>
        <el-tooltip effect="dark" content="刷新" placement="top">
          <el-button
            icon="el-icon-refresh"
            size="mini"
            circle
            @click="brush"
          ></el-button>
        </el-tooltip>
      </el-row>
    </section>
    <section class="ape-admin-layout" scroll>
      <el-table
        size="mini"
        v-loading="loading"
        element-loading-text="拼命加载中"
        height="100%"
        :data="tableData"
        tooltip-effect="dark"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center">
        </el-table-column>
        <el-table-column
          prop="tableType"
          label="表类型"
          width="100"
          align="center"
          :formatter="tableTypeFormatter"
        >
        </el-table-column>
        <el-table-column
          prop="tableName"
          label="表名"
          width="200"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="tableDescribe"
          label="表描述"
          align="center"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="version"
          label="版本"
          sortable
          width="80"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="syncStatus"
          label="同步状态"
          width="100"
          align="center"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.syncStatus === 1">已同步</el-tag>
            <el-tag v-if="scope.row.syncStatus === 0" type="danger">
              未同步
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="updateTime"
          label="更新时间"
          width="200"
          align="center"
          sortable
        >
        </el-table-column>
        <el-table-column label="操作" width="160" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              size="mini"
              @click="handleUpdate(scope.row)"
            >
              编辑
            </el-button>
            &nbsp;
            <el-dropdown size="mini" @command="handleMore">
              <el-button type="text" size="mini">
                更多<i class="el-icon-arrow-down"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item
                  icon="el-icon-view"
                  :command="{ lab: 1, val: scope.row }"
                >
                  预览
                </el-dropdown-item>
                <el-dropdown-item
                  icon="el-icon-refresh"
                  :command="{ lab: 2, val: scope.row }"
                >
                  同步
                </el-dropdown-item>
                <el-dropdown-item
                  icon="el-icon-download"
                  :command="{ lab: 3, val: scope.row }"
                >
                  生成代码
                </el-dropdown-item>
                <el-dropdown-item
                  icon="el-icon-delete"
                  divided
                  :command="{ lab: 4, val: scope.row }"
                >
                  删除
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </section>
    <section
      class="ape-admin-layout"
      type="flex"
      align="center"
      position="bottom"
    >
      <Pagination
        :show="total > 0"
        :page.sync="queryParams.pageNum"
        :size.sync="queryParams.pageSize"
        :total="total"
        @change="getList"
      ></Pagination>
    </section>
    <!-- 添加或修改对话框 -->
    <edit-dialog
      :open.sync="open"
      :title.sync="title"
      :editForm="form"
      @success="getList"
    ></edit-dialog>
    <!-- 代码预览对话框 -->
    <preview-code :show.sync="previewCodeShow"></preview-code>
  </div>
</template>

<script>
import editDialog from "./editDialog.vue";
import previewCode from "./previewCode.vue";
import { getTable, getTableInfo, deleteTable } from "@/api/system/tool";
export default {
  components: {
    editDialog,
    previewCode,
  },

  data() {
    return {
      // 是否显示搜索条件
      showSearch: true,
      // 加载遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tableType: null,
        tableName: null,
        tableDescribe: null,
        syncStatus: null,
      },
      // 总条数
      total: 0,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 表格数据
      tableData: [],
      /**
       * -----------------------
       */
      /**
       * 表单参数
       */
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单数据表字段选中数组
      tableFiledSelectList: [],
      // 表单参数
      form: null,
      /**
       * -----------------------
       */
      // 代码预览
      previewCodeShow: false,
    };
  },

  created() {
    this.getList();
  },

  mounted() {},

  methods: {
    /** 查询数据列表 */
    getList() {
      this.loading = true;
      getTable(this.queryParams)
        .then((res) => {
          this.loading = false;
          if (res.code === 200) {
            this.tableData = res.data.list;
            this.total = res.data.total;
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch((err) => {
          this.loading = false;
          this.$message.error(err);
        });
    },
    /** 搜索按钮操作 */
    handleSearch() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.tableType = null;
      this.queryParams.tableName = null;
      this.queryParams.tableDescribe = null;
      this.queryParams.syncStatus = null;
      this.resetForm("queryForm");
      this.handleSearch();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form = null;
      this.open = true;
      this.title = "添加数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      const id = row.id || this.ids[0];
      getTableInfo({
        id: id,
      })
        .then((res) => {
          this.loading = false;
          if (res.code === 200) {
            this.form = res.data;
            this.open = true;
            this.title = "修改数据";
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch((err) => {
          this.loading = false;
          this.$message.error(err);
        });
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let ids = row.id || this.ids;
      ids = Array.isArray(ids) ? ids : [ids];
      this.$msgbox({
        title: "",
        message: "此操作将永久删除选中的数据, 是否继续?",
        showClose: false,
        confirmButtonText: "确定",
        showCancelButton: true,
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteTable({ ids: ids })
            .then((res) => {
              if (res.code === 200) {
                this.$message({
                  type: "success",
                  message: res.msg,
                });
              } else {
                this.$message.error(res.msg);
              }
              this.brush();
            })
            .catch((err) => {
              this.$message.error(err);
            });
        })
        .catch(() => {});
    },
    /** 更多按钮操作 */
    handleMore(e) {
      switch (e.lab) {
        case 1:
          this.previewCodeShow = true;
          break;
        case 2:
          break;
        case 3:
          break;
        case 4:
          this.handleDelete(e.val);
          break;
        default:
          return;
      }
    },
    /** 刷新 */
    brush() {
      this.getList();
    },
    // 类型格式化
    tableTypeFormatter(row) {
      switch (row.tableType) {
        case 1:
          return "单表";
        case 2:
          return "主表";
        case 3:
          return "附表";
        default:
          return "";
      }
    },
  },
};
</script>
